package com.shujia.core.transformations

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object FilterOpt {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
    conf.setMaster("local")
    conf.setAppName("filter算子操作")

    val sc = new SparkContext(conf)

    val lineRDD: RDD[String] = sc.textFile("spark/data/students.txt")

    //过滤出性别是男生的
    /**
     * filter算子，根据用户传入的过滤条件，当条件为true的时候，将这个元素整体保留下来，反之，删掉
     */
    val nanRDD: RDD[String] = lineRDD.filter((line: String) => {
      var flag:Boolean = false
      val gender: String = line.split(",")(3)

      if("男".equals(gender)){
        println("================ 男生 保留 =====================")
        flag = true
      }else{
        println("================ 女生 删除 =====================")
      }
      flag
    })

    nanRDD.foreach(println)


  }
}
